package com.superzqb.asus.superzqbFragment.superzqbJobShop.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

import com.superzqb.asus.superzqb.R;
import com.superzqb.asus.superzqbFragment.superzqbJobShop.Shop.CampusGoodsDetail;
import com.superzqb.asus.superzqbUtil.StyleGroup;

import java.util.ArrayList;
import java.util.HashMap;

import static android.media.CamcorderProfile.get;

/**
 * Created by asus on 2017/1/27.
 */

public class StyleListAdapter extends BaseAdapter {
    private Context mContext;
    private TextView choose;
    private ArrayList<HashMap<String,Object>> mList;
    private int size=0;//商品属性是1类还是两类
    private ArrayList<String> tmp1=new ArrayList<>();//存类别名
    private ArrayList<String> tmp=new ArrayList<>();//存该类别下的属性
    private  ArrayList<String> sub=new ArrayList<>();//提交的时候传送的数据
    private CampusGoodsDetail cam=new CampusGoodsDetail();
    //用于保存用户的属性集合
    private HashMap<String,String> selectProMap=new HashMap<String, String>();
    public int getSize() {
        return size;
    }

    public ArrayList<String> getSub() {
        return sub;
    }
    public HashMap<String, String> getSelectProMap() {
        return selectProMap;
    }
    public void setSelectProMap(HashMap<String, String> selectProMap) {
        this.selectProMap = selectProMap;
    }
    public StyleListAdapter(Context context, ArrayList<HashMap<String, Object>> list, TextView choose){
        super();
        this.choose=choose;
        this.mContext=context;
        if(mList==null)
        this.mList=list;
    /*    mViewList=new ArrayList<HashMap<String,TextView[]>>();
        drawable=mContext.getResources().getDrawable(R.drawable.button);*/
    }
    @Override
    public int getCount() {
        return mList==null?0:mList.size();
    }
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            // 获取list_item布局文件的视图
            convertView = LayoutInflater.from(this.mContext).inflate(R.layout.campus_shop_goods_buy_item , null);
            holder = new ViewHolder();
            // 获取控件对象
            holder.tvPropName= (TextView) convertView
                    .findViewById(R.id.name);
            //holder.llPropContents=(LinearLayout)convertView.findViewById(R.id.ll_property_content);
            //holder.tlPropContents=(TableLayout)convertView.findViewById(R.id.ll_property_content);
            // 设置控件集到convertView
            holder.vgPropContents= (StyleGroup) convertView.findViewById(R.id.mygroup);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (this.mList != null) {
            if(this.mList.size()==1) {
                size = 1;
                sub.add("-1");
                sub.add("-1");
            }else if(this.mList.size()==2) {
                size = 2;
                sub.add("-1");
            }
            else size=0;
            //HashMap<String,TextView[]> mapView=new HashMap<String, TextView[]>();
            ArrayList<String> lables = (ArrayList<String>) this.mList.get(position).get("label");
            String type = (String) this.mList.get(position).get(
                    "type");
            tmp1.add(type);
            tmp.add("1");
            holder.tvPropName.setText(type);//规格名称
            //动态加载标签
            //判断布局中的子控件是否为0，如果不为0，就不添加了，防止ListView滚动时重复添加
            if(holder.vgPropContents.getChildCount()==0){
                TextView[]  textViews = new TextView[lables.size()];
                //设置每个标签的文本和布局
                //TableRow tr=new TableRow(mContext);
                for (int i = 0; i < lables.size(); i++) {
                    TextView textView = new TextView(mContext);
                    textView.setGravity(1);
                    textView.setPadding(20,10,20,10);
                    textViews[i] = textView;
                    textViews[i].setBackgroundResource(R.drawable.button);
                    textViews[i].setText(lables.get(i));
                    textViews[i].setTag(i);
                    //textViews[i].setBackgroundColor(Color.parseColor("#EE5500"));
                    //tr.addView(textViews[i]);
                    // holder.llPropContents.addView(textViews[i]);
                    holder.vgPropContents.addView(textViews[i]);
                }
                //holder.tlPropContents.addView(tr);
                //绑定标签的Click事件
                for(int j=0;j<textViews.length;j++){
                    textViews[j].setTag(textViews);
                    textViews[j].setOnClickListener(new LableClickListener(type));
                }

                //把控件存起来
//               mapView.put(type, textViews);
//               mViewList.add(mapView);
            }
            /**判断之前是否已选中标签*/
            if(selectProMap.get(type)!=null){
                for(int h=0;h<holder.vgPropContents.getChildCount();h++){
                    TextView v=(TextView) holder.vgPropContents.getChildAt(h);
                    if(selectProMap.get(type).equals(v.getText().toString())){
                        v.setBackgroundColor(Color.parseColor("#EE5500"));
                        v.setTextColor(Color.parseColor("#FFFFFF"));
                        selectProMap.put(type, v.getText().toString());
                    }
                }
            }
        }
        return convertView;
    }
    /*定义item对象*/
    public class ViewHolder {
        TextView tvPropName;
        LinearLayout llPropContents;
        StyleGroup vgPropContents;
        TableLayout tlPropContents;
    }

    class LableClickListener implements View.OnClickListener {
        private String type;
        public LableClickListener(String type){
            this.type=type;
        }
        @Override
        public void onClick(View v) {
            TextView[] textViews=(TextView[])v.getTag();
            TextView tv=(TextView)v;
            for(int i=0;i<textViews.length;i++){
                //让点击的标签背景变成橙色，字体颜色变为白色
                if(tv.equals(textViews[i])){
                    textViews[i].setBackgroundColor(Color.parseColor("#EE5500"));
                    textViews[i].setTextColor(Color.parseColor("#FFFFFF"));
                    selectProMap.put(type,textViews[i].getText().toString() );
                    /*接下来是处理选择商品属性的逻辑*/
                    if(size==1) {
                        choose.setText("已选择:'" + textViews[i].getText().toString() + "'");
                        sub.set(0,i+"");
                    }else if(size==2) {
                        for (int k = 0; k < tmp1.size(); k++) {
                            if (type.equals(tmp1.get(k))) {
                                tmp.set(k, textViews[i].getText().toString());
                                sub.set(k, i + "");
                            }
                        }
                        if ((tmp.get(0).equals("1") && !tmp.get(1).equals("1")) || (tmp.get(1).equals("1") && !tmp.get(0).equals("1"))) {
                            choose.setText("已选择:'" + textViews[i].getText().toString() + "'");

                        } else {
                            choose.setText("已选择:'" + tmp.get(0) + "','" + tmp.get(1) + "'");
                        }
                    }
                        cam.changeSub(sub);
                       /* if(isFirst){
                            tmp=textViews[i].getText().toString();
                            isFirst=false;
                            same=type;
                            choose.setText("已选择:'"+textViews[i].getText().toString()+"'");
                        }else{
                            if(same.equals(type)){
                                tmp=textViews[i].getText().toString();
                                choose.setText("已选择:'"+textViews[i].getText().toString()+"'");
                            }else{
                                tmp1=textViews[i].getText().toString();
                                same=type;
                                choose.setText("已选择:'"+tmp+"','"+tmp1+"'");
                            }
                        }*/

                }else{
                    //其他标签背景变成白色，字体颜色为黑色
                    //textViews[i].setBackgroundDrawable(drawableNormal);
                    textViews[i].setBackgroundResource(R.drawable.button);
                    textViews[i].setTextColor(Color.parseColor("#000000"));
                    /*textViews[i].setBackgroundColor(Color.parseColor("#ffffff"));*/
                }
            }
        }
    }
}

